<template>
  <section class="category-box pro_second">
    <div class="category-item knife_container">
      <div class="category-row" :class="[collapseCate ? '':'isActive']">
        <span class="category-tit">分类</span>
        <section class="category-cont" ref="refCategory">
          <div class="collose-tag collose-pos" @click="collapseCate = !collapseCate">{{collapseCate ? '收起':'展开'}} <i :class="[collapseCate ? 'el-icon-arrow-up':'el-icon-arrow-down']"></i></div>
          <div class="cont-item">
            <a v-for="(item, index) in firstCategoryList" @click="clickCategory(0, item.id, index)" :key="index" :class="{'active': item.id === activeFir}" class="cont-item_a" :data-id="item.id" >
              {{item.name}}
            </a>
          </div>
          <div class="cont-item" v-if="activeFir !== null">
            <a class="cont-item_a" :class="{'active': activeSec === 'all'}" @click="clickCategory(0, activeFir)"> 全部</a>
            <a class="cont-item_a" v-for="(secitem, secindex) in secCategoryList" :key="secindex"
              :class="{'active': secitem.id === activeSec}" :data-id="secitem.id"  @click="clickCategory(1, secitem.id, secindex)">{{secitem.name}}</a>
          </div>
          <div class="cont-item" v-if="activeSec !== 'all' && activeSec !== null">
            <a class="cont-item_a" :class="{'active': activeTh === 'all'}" @click="clickCategory(1, activeSec)"> 全部</a>
            <a class="cont-item_a" v-for="(thirdItem, thirdIndex) in thirdCategoryList" :key="thirdIndex" 
            :class="{'active': thirdItem.id === activeTh}" :data-id="thirdItem.id" @click="clickCategory(2, thirdItem.id, thirdIndex)">{{thirdItem.name}}
            </a>
          </div>
        </section>
      </div>
      <!-- 品牌 v-if="brandHeight > 60"-->
      <div class="category-row" v-if="brandList.length > 0">
        <span class="category-tit">品牌</span>
        <section class="category-cont" :class="[collapseBrand ? '':'isActive']">
          <div class="collose-tag collose-pos" v-if="brandHeight > 60" @click="collapseBrand = !collapseBrand">{{collapseBrand ? '收起':'展开'}} <i :class="[collapseBrand ? 'el-icon-arrow-up':'el-icon-arrow-down']"></i></div>
          <div class="cont-item brand-item" ref="refBrand" >
            <a class="cont-item_a" :class="{'active': brandId === ''}" @click="clickBrand('')">全部品牌</a>
            <a class="cont-item_a" v-for="(brands, brandIndex) in brandList" :key="brandIndex" :data-id="brands.id" 
            :class="{'active': brands.id === brandId}" @click="clickBrand(brands.id)">{{brands.name}}</a>
          </div>
        </section>
      </div>
    </div>
  </section>
</template>
<script>
  // import _ from 'lodash'
  import {Tabs, TabPane} from 'element-ui'
  import {getCategory} from '@/service/get-data'
  import {eventBus} from '@/tool/events-bus'
  export default {
    name: 'secondMenu',
    components: {
      elTabs: Tabs,
      elTabPane: TabPane
    },
    props: {
      brandList: {
        type: Array,
        default: []
      }
    },
    data () {
      return {
        threeCategoryObj: {}, // 三层类目对象
        allCategoryList: [], // 全部分类
        firstCategoryList: [], // 一级分类
        secCategoryList: [], // 二级分类
        thirdCategoryList: [], // 三级
        allCategoryVisi: false, // 全部分类显示
        activeFir: null, // 一级点亮的id值
        activeSec: null, // 二级点亮的id值
        activeTh: 'all', // 三级点亮的id值
        // 分类
        collapseCate: false, // 分类是否展开
        collapseBrand: this.$route.query.collbrand === 'true', // 品牌是否展开 JSON.parse(this.$route.query.collbrand) ||
        brandId: +this.$route.query.brand || '', // 当前选中的品牌id
        keyword: '',
        brandHeight: '',
        cateHeight: '' // 类目的高度
      }
    },
    methods: {
      // 品牌点击
      clickBrand (brandId) {
        let routequery = this.$route.query
        this.brandId = brandId
        this.$router.push({name: 'ProductList', query: {typeId: routequery.typeId || '', type: routequery.type || '', firstId: routequery.firstId || '', secId: routequery.secId || '', thirdId: routequery.thirdId || '', brand: this.brandId, keyword: routequery.keyword || ''}})
        this.$emit('child-brand', brandId)
      },
      // 获取类目列表接口 三层级
      async fetchGetCategory () {
        let loading = this.$loading({target: '.category-section'})
        let res = await getCategory()
        if (res.data.resultCode === 2000) {
          loading.close()
          res = res.data
          this.allCategoryList = res.data
        } else {
          this.$message({
            message: res.data.resultMsg
          })
        }
      },
      // 获取二级类目,和三级类目
      getChildrenCategory (firIndex = 0, secIndex = 0) {
        let secList = []
        let secCopyList = []
        let thirdList = []
        let thirdCopyList = []
        if (this.allCategoryList.length <= 0) return
        secCopyList = this.allCategoryList.filter((firItem, filterIndex) => {
          return filterIndex === firIndex
        })
        secCopyList[0].children.map((secItem, secMapIndex) => {
          secList.push(secItem)
        })
        thirdCopyList = secList[secIndex]
        thirdCopyList.children.map((thirdItem, thirdIndex) => {
          thirdList.push(thirdItem)
        })
      },
      setActiveName () {
        let queryobj = this.$route.query
        this.activeFir = queryobj.firstId - 0 || null
        this.activeSec = queryobj.secId - 0 || 'all'
        this.activeTh = queryobj.thirdId - 0 || 'all'
      },
      // 点击类目
      clickCategory (type, id, index) {
        let routequery = this.$route.query
        this.keyword = routequery.keyword || ''
        let all = this.allCategoryList
        let lastIndex = index
        const actions = new Map([
          [0, () => {
            if (!index) { // 点击二级的全部
              all.map((item, itemIndex) => {
                if (item.id === id) {
                  lastIndex = itemIndex
                }
              })
            }
            this.secCategoryList = all[lastIndex].children
            this.activeFir = id
            this.activeSec = 'all'
            this.activeTh = 'all'
            this.$router.push({name: 'ProductList', query: {typeId: id, type: type, firstId: this.activeFir, keyword: this.keyword, brand: this.brandId}})
          }],
          [1, () => {
            if (!index) { // 点击三级的全部
              this.secCategoryList.map((secItem, secItemIndex) => {
                if (secItem.id === id) {
                  lastIndex = secItemIndex
                }
              })
            }
            this.thirdCategoryList = this.secCategoryList[lastIndex].children
            this.activeSec = id
            this.activeTh = 'all'
            this.$router.push({name: 'ProductList', query: {typeId: id, type: type, firstId: this.activeFir, secId: this.activeSec, keyword: this.keyword, brand: this.brandId}})
          }],
          [2, () => {
            this.activeTh = id
            this.$router.push({name: 'ProductList', query: {typeId: id, type: type, firstId: this.activeFir, secId: this.activeSec, thirdId: id, keyword: this.keyword, brand: this.brandId}})
          }]
        ])
        let action = actions.get(type)
        action.call(this)
        this.$emit('click-child', id, type)
      },
      // 监听搜索框的事件
      watchEvent () {
        const _this = this
        eventBus.$on('search', function (val) {
          _this.activeFir = null
          _this.activeSec = null
          _this.activeTh = null
        })
      }
    },
    // TODO: 监听展开和收起
    computed: {
      categoryLink () {
        if (this.activeSec) {
          let res = this.activeFir + this.activeSec + this.activeTh
          return res
        }
      }
    },
    mounted () {
      this.fetchGetCategory() // 获取所有的类目
      // this.getChildrenCategory()
      this.setActiveName()
      this.watchEvent()
    },
    watch: {
      // 二级菜单非空的时候自动展开
      secCategoryList (newVal) {
        if (newVal.length !== 0) {
          this.collapseCate = true
        }
      },
      allCategoryList (newVal) {
        let _arr = []
        let _secarr = []
        let _thirdarr = []
        newVal.map((firItem, firIndex) => {
          _arr.push(firItem)
        })
        this.firstCategoryList = _arr
        // 二级
        _secarr = newVal.filter((firItem, filterIndex) => {
          // 根据id 获取一级类目的名字
          return firItem.id === parseInt(this.activeFir)
        })
        if (_secarr.length > 0) {
          this.secCategoryList = _secarr[0].children
        }
        if (this.activeSec === 'all') {
          this.thirdCategoryList = []
        } else {
          _thirdarr = this.secCategoryList.filter((tItem, tIndex) => {
            return tItem.id === parseInt(this.activeSec)
          })
        }
        if (_thirdarr.length > 0) {
          this.thirdCategoryList = _thirdarr[0].children
        }
      },
      // TODO: // 展开与收起的显示
      // 品牌类目的高度
      brandList (val) {
        this.$nextTick(() => {
          if (this.$refs.refBrand) {
            let height = this.$refs.refBrand.offsetHeight
            this.brandHeight = height
          }
        })
      },
      // 二级分类的高度
      categoryLink () {
        this.$nextTick(() => {
          if (this.$refs.refCategory) {
            let height = this.$refs.refCategory.offsetHeight
            this.cateHeight = height
          }
        })
      }
    }
  }
</script>
<style lang="scss" scoped>
  .pro_second{
    margin-bottom: 20px;
    // margin-top: 30px;
    padding-top: 30px;
  }
  a{
    &:hover{
      color:#FFCC00;
    }
  }
  .active{
    color:#FFCC00;
  }
  .category-section{
    background:#ffffff;
    box-shadow:0 2px 5px 0 rgba(0,0,0,0.05);
    border-radius:2px;
    .category-div{
      overflow: hidden;
      padding-left:85px;
      padding-right: 113px;
      border-bottom: 1px solid #dddddd;
      padding-top: 20px;
      width: calc(100% - 194px);
      font-size: 14px;
      &:last-child{
        border-bottom:none;
      }
    }
  }
  .category-first{
    padding-left: 20px;
    height: 60px;
    line-height: 60px;
    font-size:16px;
    border-bottom: 1px solid #dcdcdc;
    .title{
      display: inline-block;
      padding-right: 30px;
    }
    .item{
      padding-right: 27px;
    }
    .title_a{
      padding-right: 27px;
      color:#000;
      &.active{
        color:#FFCC00;
      }
    }
  }
  .content{
      display: inline-block;
      margin-left: 0;
    }
  .category-div{

    // background: #f3f3f3;
    // .title{
    //   float: left;
    //   width: 100px;
    //   padding-left: 10px;
    // }
    .content{
      display: inline-block;
      margin-left: 0;
    }
    .arrow{
      position: absolute;
      right: 10px;
      top: 0;
      font-size: 12px;
      width: 50px;
      text-align: center;
      cursor: pointer;
    }
  }
  .J_valueList a{
    float: left;
    // width: 80px;
    padding-bottom: 20px;
    padding-right: 25px;
    font-size: 14px;
    cursor: pointer;
    &:hover{
      color:#FFCC00
    }
  }
  .J_valueList_li.active{
    color:#FFCC00
  }
  .F_valuelist li a{
    width: 130px;
  }
  .S_valueList{
    height: 33.6px;
    transition: all 0.2s ease-in-out;
  }
  .S_valueList.show{
    height: auto;
  }
      // 分类 
    .category-box {
      position: relative;
      .category-row{
        overflow: hidden;
        // display: flex;
        border-bottom: 1px solid #e6e6e6;
        // justify-content: flex-start;
        transition: all 0.3s;
        background: #ececec;
      }
      
      .category-tit {
        // display: table-cell;
        float: left;
        background:#ececec;
        text-align: center;
        color: #858585;
        width:90px;
        line-height: 60px;
        font-size: 14px;
      }
      .category-cont{
        // display: table-cell;
        position: relative;
        // flex: 1;
        background: #fff;
        height: auto;
        margin-left: 90px;
      }
      .cont-item {
        // width: 1110px;
        //height: 60px;
        // line-height: 60px;
        border-bottom: 1px solid #e6e6e6;
        padding: 10px 150px 10px 0;
        &:last-child {
          border-bottom: none;
        }
      }
      .cont-item_a{
        color: #666;
        font-size: 14px;
        display: inline-block;
        padding: 13px 30px 13px 32px;
        &.active{
          color:#FFCC00;
        }
      }
       .isActive{
        height: 60px;
        overflow: hidden;
      }
    }
    .collose-tag{
      font-size: 12px;
      color: #999;
      cursor: pointer;
      width: 60px;
      height: 30px;
      line-height: 30px;
      text-align: center;
    }
    .collose-pos{
      position: absolute;
      right: 10px;
      top: 10px;
    }
</style>
